<?php

//TODO://FIX://INFO: Шура! Вы Мистер Распизьдяйство 2009!!! Переписывать всё нахуй, бля. Самокритично.


$agents = array(
'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; MRA 5.5 (build 02780); MRSPUTNIK 2, 1, 0, 7 SW; .NET CLR 1.1.4322)',
'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; GTB6; .NET CLR 1.1.4322; InfoPath.1)',
'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; InfoPath.2)',
'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; MRA 4.3 (build 01218); .NET CLR 1.1.4322)',
'Mozilla/5.0 (Windows; U; Windows NT 6.0; ru; rv:1.9.1) Gecko/20090624 MRA 5.5 (build 02812) Firefox/3.5 ',
'Opera/9.63 (Windows NT 5.1; U; MRA 5.3 (build 02552); ru) Presto/2.1.1',
'Mozilla/4.0 (compatible; MSIE 5.01; Windows 95; MSIECrawler)',
'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; {CSC}; FunWebProducts; .NET CLR 2.0.50727; .NET CLR 1.1.4322)',
'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; InfoPath.1',
'Mozilla/5.0 (Windows; U; WinNT4.0; en-US; rv:1.5) Gecko/20031016 K-Meleon/0.8',
'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 4.0)',
'Mozilla/5.0 (Windows; U; Windows NT 6.0; ru; rv:1.9.0.11) Gecko/2009060215 Firefox/3.0.11 (.NET CLR 3.5.30729) ',
'Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.9.0.11) Gecko/2009060215 MRA 5.5 (build 02793) Firefox/3.0.11 sputnik 2.0.1)',
'msnbot-media/1.1 (+http://search.msn.com/msnbot.htm)'
);

/*
 * hazg.net 2008
 */
	class Web 
	{
		const GET_TYPE_PHP = 0;
		const GET_TYPE_SPY = 1; 
		const GET_FILE_ALL = 0;
		const GET_FILE_BODY = 1;
		//const GET_FILE_HEADER = 2;
		static function Get($url, $post = null, $get_type = Web::GET_TYPE_SPY, $get_file = Web::GET_FILE_BODY, $round = 0) 
		{
			
			//print "\nWeb::Get($url);\n";
			if(empty($url)) return false;
			if($get_type = Web::GET_TYPE_PHP)
				return file_get_contents($url);
			return Web::SendHeader($url, Web::GetHeader($url, $post), $get_file, $round);
		}
				
		static function SendHeader($host, $head, $get_file, $round)
		{
			
			$p_url = parse_url($host);
			if(!$p_url) return false;
			$fp = fsockopen($p_url['host'], 80, $errno, $errstr, 20);
			fwrite($fp, $head, strlen($head));
			$response = '';			 
			while(false !== ($buf = fgets($fp)))
			{
				$response.=$buf;
			}
			/*Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: close
set-cookie: session=5F3F5D4E09E05454; expires=Wed, 22 Oct 2008 11:03:32 GMT; path=/
Content-Encoding: gzip*/
			$headerPos = stripos($response, chr(0x0D).chr(0x0A).chr(0x0D).chr(0x0A));
			$header = substr($response, 0, $headerPos);
			$body = substr($response, $headerPos+4);
			//TODO: Content-Type iconv
			unset($response);
			if(preg_match('#.*Content-Encoding:\sgzip.*#is', $header))
				$body = gzinflate(substr($body,2));
			
					
			 
			fclose($fp);
			if(stripos($header, 'HTTP/1.1 302')!== false)
			{
				//FIX:HACK!!!!!
				file_put_contents('1', $header);
				if(preg_match('#^.*?Location:(.*?)(\n|\r|$)#mis', $header, $matches))
				{
					if($round < 10)
						return Web::Get(trim($matches[1]), null, Web::GET_TYPE_SPY, $get_file, $round++);
					else return '';
				}
			}
			if($get_file === Web::GET_FILE_ALL)
	    		return $header.chr(0x0D).chr(0x0A).chr(0x0D).chr(0x0A).$body;
	    	else if($get_file === Web::GET_FILE_BODY)
	    		return $body;
		}
		
		
		static function GetHeader($url, $post)
		{
			$p_url = parse_url($url);
			if(!$p_url) return false;
			$q = (array_key_exists('query', $p_url)?("?".$p_url['query']):'');
			if($post == null)
			{
				$u = str_replace(' ', '%20', empty($p_url['path'])?'':$p_url['path']);
				if(empty($u)) $u = '/';

				$header = "GET ".$u.$q." HTTP/1.1\r\n";
				$header .= "Accept: */*\r\n";
				$header .= "Accept-Language: ru\r\n";
				//$header .= "Accept-Encoding: gzip, deflate\r\n";
				global $agents;
				$header .= "User-Agent: ".$agents[rand(0, sizeof($agents))]."\r\n";
				//$header .= "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)\r\n";
				$header .= "Host: ".$p_url['host']."\r\n";
				//$header .= "Connection: Keep-Alive\r\n";
				$header .= "Connection: Close\r\n\r\n";
				
			}
			else 
			{
				$query = http_build_query($post);
				$u = str_replace(' ', '%20', $p_url['path']);
				$header = "POST ".$u.$q." HTTP/1.1\r\n". 
				"Host: ".$p_url['host']."\r\n".
				"User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)\r\n". 
				"Content-Type: application/x-www-form-urlencoded\r\n".
				"Connection: Close\r\n". 
				"Content-Length: ".strlen($query)."\r\n\r\n". 
				"$query\r\n";
			}
			return $header;
		}
	}
	
?>